<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\index\controller;

/**
 * Description of ApiAdmin
 *
 * @author Dongasai
 */
class ApiAdmin extends \think\Controller
{

    public function index()
    {
        $cate_id = input('cate_id', 0, 'int');
        if (!$cate_id) {
            return $this->error("缺少必要参数!");
        }
        //读取接口列表
        $page  = input("page", 1, 'int');
        $uid   = session('uid');
        $order = '';
        $data  = \app\index\service\Api::listForPage($uid, $cate_id, $order, $page, 10);

        $this->assign('api', dataToPagedata($data));
        return $this->fetch();
    }

    public function edit()
    {
        if (request()->ispost()) {
            //进行编辑处理
            $data = $_POST;
            $uid  = session('uid');
            $re   = \app\index\service\Api::edit($uid, $data);
            if (is_string($re)) {
                return $this->error($re);
            } else {
                return $this->success("成功");
            }
        } else {
            //展示信息
            $id      = input("id", 0, 'int');
            $apiInfo = \app\index\service\Api::info($id);
            $this->assign('apiInfo', $apiInfo);
            return $this->fetch();
        }
    }

    public function add()
    {
        $cate_id = input('cate_id', 0, 'int');
        $uid     = session('uid');
        if (request()->ispost()) {
            //处理数据
            $data            = $_POST;
            $data['cate_id'] = $cate_id;
            $re              = \app\index\service\Api::add($uid, $data);
            if (is_string($re)) {
                return $this->error($re);
            } else {
                return $this->success("成功!");
            }
        } else {
            //展示页面
            return $this->fetch();
        }
    }

}
